package top.yifengxiner.blockchain.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import top.yifengxiner.blockchain.mapper.EducatedMapper;
import top.yifengxiner.blockchain.pojo.entity.Educated;
import top.yifengxiner.blockchain.pojo.vo.BRegisterBody;
import top.yifengxiner.blockchain.pojo.vo.EducatedVo;
import top.yifengxiner.blockchain.service.EducatedService;
import top.yifengxiner.common.core.pojo.AjaxResult;
import top.yifengxiner.common.core.pojo.entity.SysUser;
import top.yifengxiner.framework.shiro.service.SysPasswordService;
import top.yifengxiner.system.mapper.SysUserMapper;
import top.yifengxiner.system.service.SysUserService;
import java.util.List;

/**
 * @author ：baixiao
 * @date ：Created in 2021/5/12 10:02
 * @description：受教育者服务
 * @modified By：
 * @version: 0.0.1$
 */
@Service
public class EducatedServiceImpl implements EducatedService
{
    @Autowired
    private EducatedMapper educatedMapper;

    @Autowired
    private SysUserService userService;

    @Autowired
    private SysUserMapper userMapper;

    @Autowired
    private SysPasswordService passwordService;

    @Override
    public Educated getEducatedById(Long id)
    {
        Educated educated = educatedMapper.selectOne(new QueryWrapper<Educated>().eq("educated_id",id));
        return  educated;
    }

    @Override
    public List<Educated> getListById(Long educatorId)
    {
        List<Educated> list = educatedMapper.selectList(new QueryWrapper<Educated>().eq("educator_id",educatorId));
        return list;
    }

    @Override
    public int setEducated(BRegisterBody registerBody)
    {
        SysUser user = userService.selectUserByUsername(registerBody.getSubject());
        Educated educated = new Educated();
        educated.setEducatedId(user.getUserId());
        educated.setUsername(user.getUserName());
        educated.setPassword(user.getPassword());
        int res = educatedMapper.insert(educated);
        return res;
    }

    @Override
    public AjaxResult updateEducated(EducatedVo educatedVo)
    {
        SysUser sysUser = userMapper.selectById(educatedVo.getEducatedId());

        SysUser user = new SysUser();
        user.setUserId(educatedVo.getEducatedId());
        user.setUserName(educatedVo.getUsername());
        user.setPassword(passwordService.encryptPassword(educatedVo.getUsername()
                ,educatedVo.getPassword()
                ,sysUser.getSalt()));
        int re = userMapper.update(user,new UpdateWrapper<SysUser>().eq("user_id",educatedVo.getEducatedId()));
        if(re != 1)
        {
            return AjaxResult.error("操作出错");
        }
        Educated educated = new Educated();
        educated.setEducatedId(educatedVo.getEducatedId());
        educated.setUsername(educatedVo.getUsername());
        educated.setPassword(passwordService.encryptPassword(educatedVo.getUsername()
                ,educatedVo.getPassword()
                ,sysUser.getSalt()));
        educated.setEducatorId(userService.selectUserByUsername(educatedVo.getEducatorName()).getUserId());
        educated.setEducatorName(educatedVo.getEducatorName());
        educated.setIdentity(educatedVo.getIdentity());
        int res = educatedMapper.update(educated,new UpdateWrapper<Educated>().eq("educated_id",educatedVo.getEducatedId()));
        if(res ==1)
        {
            return AjaxResult.success("修改成功");
        }
        return AjaxResult.error("修改错误");
    }

    @Override
    public Educated getEducatedByName(String name)
    {
        return educatedMapper.selectOne(new QueryWrapper<Educated>().eq("user_name",name));
    }
}
